{include file="public/header"/}
</head>
<body>
<div class="layui-form">
<div class="layui-fluid" id="LAY-component-layer-list">
<div class="layui-col-md12 pd83">
<div class="layui-card hideshadow">
  <div class="layui-tab layui-tab-brief">
  {include file="web/top"}
  <div class="layui-tab-content">
    <div class="layui-card-header">邮件配置</div>
    <div class="layui-form-item">
        <label class="layui-form-label pcfcms_layui-form-label">状态</label>
        <div class="layui-input-inline pcfcms_layui-input-inline">
            <input type="checkbox" name="smtp_syn_weapp" lay-filter="smtp_syn_weapp" value="{$config.smtp_syn_weapp|default='0'}" lay-skin="switch" lay-text="开启|关闭" {notempty name="$config.smtp_syn_weapp"}checked{/notempty}>
        </div>
    </div>
    <div class="layui-form-item" style="margin-bottom:0;margin-top:0;">
        <label class="layui-form-label pcfcms_layui-form-label"><b class="pcfts">*</b> SMTP服务器</label>
        <div class="layui-input-inline pcfcms_layui-input-inline">
            <input type="text" name="smtp_server" id="smtp_server" lay-verify="required" value="{$config.smtp_server}" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label pcfcms_layui-form-label"></label>
        <div class="layui-form-inline">发送邮箱的smtp地址，如: smtp.qq.com或smtp.gmail.com</div>
    </div>
    <div class="layui-form-item" style="margin-bottom:0;margin-top:0;">
        <label class="layui-form-label pcfcms_layui-form-label"><b class="pcfts">*</b> SMTP端口号</label>
        <div class="layui-input-inline pcfcms_layui-input-inline">
            <input type="text" name="smtp_port" id="smtp_port" lay-verify="required" value="{$config.smtp_port}" class="layui-input">
        </div>
    </div>  
    <div class="layui-form-item">
        <label class="layui-form-label pcfcms_layui-form-label"></label>
        <div class="layui-form-inline">
            <span style="color:red;">注意：如果使用阿里云服务器或Gmail，请将端口设为465，其他的可以尝试端口设为25</span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label pcfcms_layui-form-label"><b class="pcfts">*</b> 邮箱账号</label>
        <div class="layui-input-inline pcfcms_layui-input-inline">
            <input type="text" name="smtp_user" id="smtp_user" lay-verify="required" value="{$config.smtp_user}" class="layui-input">
        </div>
    </div>  
    <div class="layui-form-item">
        <label class="layui-form-label pcfcms_layui-form-label"><b class="pcfts">*</b> 邮箱授权码</label>
        <div class="layui-input-inline pcfcms_layui-input-inline">
            <input type="password" name="smtp_pwd" id="smtp_pwd" lay-verify="required" value="{$config.smtp_pwd}" class="layui-input">
        </div>
    </div>                  
    <div class="layui-form-item">
        <label class="layui-form-label pcfcms_layui-form-label">测试邮箱</label>
        <div class="layui-input-inline pcfcms_layui-input-inline">
            <input type="text" name="smtp_test" id="smtp_test" value="{$config.smtp_test}" class="layui-input">
        </div>
        <button class="layui-btn" lay-submit="sendEmail" lay-filter="sendEmail">发送</button>
    </div>
  </div>
  </div>
</div>
<div class="pcfclear"></div>
</div>
<div class="submit-info text-center">
    <button class="layui-btn" lay-submit lay-filter="formSubmit">确认提交</button>
</div>
</div>
</div>
{include file="public/loading"/}
{include file="public/footer"/}
<script>
    layui.use(['layer', 'form','element'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;

        // 监听开关
        form.on('switch', function(data){
            var elemId = data.elem.attributes['lay-filter']['nodeValue'];
            if (data.elem.checked) {
                this.value = 1;
            } else {
                this.value = 0;
            }
            $("#"+elemId).val(this.value);
        });

        //测试发送
        form.on('submit(sendEmail)', function(data) {
            if (data.field.smtp_test == '') {
                layer.msg('接收邮箱的地址不能为空！', {time:1000});
                $('input[name=smtp_test]').focus();
                return false;
            } else {
                layer.msg('正在处理请勿刷新页面', {icon: 16,shade: 0.01,time:3600000});
                $.ajax({
                    type: "post",
                    data: {email:data.field.smtp_test},
                    dataType: 'json',
                    url: "{:url('/web/send_email')}",
                    success: function(res) {
                        layer.closeAll();
                        if(res.status){
                            layer.msg(res.msg, {icon:1,time:1000});
                        }else{
                            layer.msg(res.msg, {icon:2,time:1000});
                        }
                    },
                    error: function() {
                        layer.closeAll();
                        layer.msg("网络中断！", {icon: 2,time:1000});
                    }
                })
            }
            return false;
        });
        
        //监听提交
        form.on('submit(formSubmit)', function(data){
            layer.msg('正在处理请勿刷新页面', {icon: 16,shade: 0.01});
            $.ajax({
                type : 'post',
                url : "{:url('/web/weapp_email')}",
                data : data.field,
                dataType : 'json',
                success : function(res){
                    layer.closeAll();
                    if(res.status){
                        layer.msg(res.msg, {icon:1,time:1000}, function(){
                            window.location.reload();
                        });
                    }else{
                       layer.msg(res.msg, {icon:2,time:1000});
                    }
                },
                error: function(e){
                    layer.closeAll();
                    layer.msg("网络中断！", {icon: 2,time:1000});
                }
            });
            return false;
        });

    });
</script>
</body>
</html>